package com.orhonit.ipb.portal.rest;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.github.ag.core.context.BaseContextHandler;
import com.orhonit.ipb.portal.biz.JoApplyJoinPartyBiz;
import com.orhonit.ipb.portal.biz.JoJoinInfoBiz;
import com.orhonit.ipb.portal.entity.JoJoinInfo;
import com.orhonit.ipb.security.auth.client.annotation.CheckUserToken;
import com.orhonit.ipb.security.common.rest.BaseController;
import com.orhonit.ipb.security.common.util.UUIDUtils;

import net.sf.json.JSONObject;

@RestController
@RequestMapping("joJoinInfo")
//@CheckClientToken
//@CheckUserToken
public class JoJoinInfoController extends BaseController<JoJoinInfoBiz,JoJoinInfo> {
	@Autowired
	private JoJoinInfoBiz joJoinInfoBiz;
	@Autowired
    private JoApplyJoinPartyBiz joApplyJoinPartyBiz;
	/**
	 * 通过申请id及标签添加
	 * @param applyId
	 * @param label
	 * @param value
	 * @return
	 */
	@RequestMapping(value = "/addjoJoinInfo", method = RequestMethod.POST)
	public ResponseEntity<?> addjoJoinInfo(String data){
		//Gson gson = new Gson();
//		JoJoinInfo joinInfo = gson.fromJson(data, new TypeToken<JoJoinInfo>() {
//		}.getType());
		JSONObject jsonObj = JSONObject.fromObject(data);
		JoJoinInfo joinInfos =new JoJoinInfo();
		joinInfos.setId(jsonObj.getString("id"));
		joinInfos.setJoinId(jsonObj.optString("joinId"));
		joinInfos.setLabel(jsonObj.getString("label"));
		joinInfos.setValue(jsonObj.optString("value"));
		if(StringUtils.isNotBlank(joinInfos.getId())){
			joJoinInfoBiz.updateById(joinInfos);
		}
		else{
			joinInfos.setId(UUIDUtils.generateUuid());
			joJoinInfoBiz.insertSelective(joinInfos);
		}
		return ResponseEntity.ok().body("1");
	}
	
	/**
	 * 通过申请id及标签查询信息
	 * @param label
	 * @return
	 */
	@RequestMapping(value = "/getjoJoinInfo", method = RequestMethod.GET)
	@CheckUserToken
	public ResponseEntity<?> getbaseUserInfoByself(String label){
//		String userId="89c75217-86af-4e62-bc11-37eb63bd273f";
		String userId =BaseContextHandler.getUserID();
		String applyId=joApplyJoinPartyBiz.getJoApplyJoinPartyByUserId(userId);
		Map<String, Object> map=new HashMap<String, Object>(); 
		map.put("joinId", applyId);
		map.put("label", label);
		JoJoinInfo joinInfo=joJoinInfoBiz.getJoJoinInfoByMap(map);
		return ResponseEntity.ok().body(joinInfo);
		 
	}
	/**
	 * 通过申请id及标签查询信息  zhibu
	 * @param label
	 * @return
	 */
	@RequestMapping(value = "/getjoJoinInfoByDept", method = RequestMethod.GET)
	public ResponseEntity<?> getjoJoinInfoByDept(String label,String JoinId){
//		String applyId=joApplyJoinPartyBiz.getJoApplyJoinPartyByUserId(userId);
		Map<String, Object> map=new HashMap<String, Object>(); 
		map.put("joinId", JoinId);
		map.put("label", label);
		JoJoinInfo joinInfo=joJoinInfoBiz.getJoJoinInfoByMap(map);
		return ResponseEntity.ok().body(joinInfo);
		 
	}
	/**
	 * 通过申请id及标签查询信息
	 * @param applyId
	 * @param label
	 * @param value
	 * @return
	 */
	@RequestMapping(value = "/updatejoJoinInfo", method = RequestMethod.POST)
	public ResponseEntity<?> updatejoJoinInfo(String applyId,String label,String value){
		JoJoinInfo joinInfo=new JoJoinInfo();
		joinInfo.setJoinId(applyId);
		joinInfo.setLabel(label);
		joinInfo.setValue(value);
		boolean a=joJoinInfoBiz.updateJoJoinInfoByMap(joinInfo);
		if(a){
			return ResponseEntity.ok().body(joinInfo);
		}
		else{
			return ResponseEntity.status(401).body("网络错误更新异常");
		}
		 
	}
}